#include "MainWindow.h"
#include "Controller.h"

#include <QMessageBox>
#include <QtGui/QIcon>
#include <QtGui/QHeaderView>
#include <iostream>

using namespace std;

#define TIMER_SESSION_INTERVAL 10
#define TIMER_STATUS_INTERVAL 15000


MainWindow::MainWindow(char* title, Controller* parent) : QMainWindow(), m_owner(parent)
{
    initialize(title);
}

MainWindow::~MainWindow()
{
    cleanup();
}

void MainWindow::initialize(char* title)
{
    // Set window title
    
    this->setWindowTitle(QString(title));

	// Set window icon

	QIcon icon("./icons/window.png");
	this->setWindowIcon(icon);
    
    // Set menubar
    
	createMenuBar();
    
    // Set layouts
    
    QWidget* centralWidget = new QWidget(this);
    this->setCentralWidget(centralWidget);
    
    centralWidget->setLayout(&m_mainLayout);    
    
    // Set Scroll Area's
   
    m_mainLayout.addWidget(&m_chatScrollArea, 1, 0);    
    m_inputLayout.addWidget(&m_inputScrollArea);
    m_infoLayout.addWidget(&m_contactScrollArea);
    m_infoLayout.addWidget(&m_infoScrollArea);

	// Set layouts

	m_mainLayout.addLayout(&m_infoLayout);
	m_mainLayout.addWidget(&m_chatScrollArea);
	m_mainLayout.addLayout(&m_inputLayout);

	// Set proportions

	m_mainLayout.setStretchFactor(&m_infoLayout, 6);
	m_mainLayout.setStretchFactor(&m_chatScrollArea, 10);
	m_mainLayout.setStretchFactor(&m_inputLayout, 1);

    // Set widgets
    
    m_infoBox = new QTextEdit();
    m_infoBox->setEnabled(true);
    m_infoScrollArea.setWidget(m_infoBox);
    m_infoScrollArea.setWidgetResizable(true);
    
    m_textDocument = new QTextDocument();
    m_infoBox->setDocument(m_textDocument);
    
    m_contactList = new QTableWidget();
	m_contactList->setSelectionMode(QAbstractItemView::SingleSelection);
    m_contactScrollArea.setWidget(m_contactList);
    m_contactScrollArea.setWidgetResizable(true);
	m_contactList->setColumnCount(1);
	QTableWidgetItem* item = new QTableWidgetItem("Contacts");
	m_contactList->setHorizontalHeaderItem(0, item);
	QHeaderView* headerView = new QHeaderView(Qt::Horizontal, m_contactList);
	m_contactList->setHorizontalHeader(headerView);
	headerView->setResizeMode(0, QHeaderView::Stretch);
    
    m_chatBox = new QTextEdit();
    m_chatBox->setEnabled(true);
    m_chatScrollArea.setWidget(m_chatBox);
    m_chatScrollArea.setWidgetResizable(true);
    
    m_inputBox = new QTextEdit();
    m_inputScrollArea.setWidget(m_inputBox);
    m_inputScrollArea.setWidgetResizable(true);
  
    m_sendButton = new QPushButton(QString("&Send"));
    m_inputLayout.addWidget(m_sendButton);

	// Timer

	m_sessionTimer = new QTimer();
	m_statusTimer = new QTimer();

	// TESTCODE

	ProfileInfo info;
	info.name = "Doggyboy";
	info.ip = "193.168.2.1";
	info.server = "didactiek2.edm.uhasselt.be";
	info.status = "online";

	setInfo(info);
}


void MainWindow::cleanup()
{
    
}

void MainWindow::startTimers()
{
	m_sessionTimer->start(TIMER_SESSION_INTERVAL);
	m_statusTimer->start(TIMER_STATUS_INTERVAL);
}

void MainWindow::addMessage(QString nick, QString message)
{
	QString toAdd = "[" + nick + "]: " + message + "\n";
	QString old = m_chatBox->document()->toPlainText();

	m_chatBox->setText(old + toAdd);
}

void MainWindow::addContact(QString nick, bool online)
{
	// Create new item

	QTableWidgetItem* item = new QTableWidgetItem();
	item->setFlags(Qt::ItemIsEnabled);

	
	if( online )
		item->setIcon(QIcon("./icons/online.png"));
	else
		item->setIcon(QIcon("./icons/offline.png"));

	item->setText(nick);
	
	// Create new row

	int row = m_contactList->rowCount();
	m_contactList->insertRow(row);
	m_contactList->setItem(row, 0, item);
}

void MainWindow::changeContactStatus(QString nick, bool status)
{
	QList<QTableWidgetItem*> items = m_contactList->findItems(nick, Qt::MatchExactly);
	QList<QTableWidgetItem*>::iterator it;

	for(it = items.begin() ; it != items.end() ; ++it)
	{
		if( status )
			(*it)->setIcon(QIcon("./icons/online.png"));
		else
			(*it)->setIcon(QIcon("./icons/offline.png"));
	}
}

void MainWindow::setInfo(ProfileInfo & info)
{
	QString text = "<h3>&nbsp;<u>USER PROFILE:</u></h3><br /><br />";
	text = text + QString("&nbsp;&nbsp;<b>Nickname:</b>&nbsp;") + QString(info.name.c_str()) + QString("<br /><br />");
	text = text + QString("&nbsp;&nbsp;<b>IP:</b>&nbsp;") + QString(info.ip.c_str()) + QString("<br /><br />");
	text = text + QString("&nbsp;&nbsp;<b>Server:</b>&nbsp;") + QString(info.server.c_str()) + QString("<br /><br />");
	text = text + QString("&nbsp;&nbsp;<b>Status:</b>&nbsp;") + QString(info.status.c_str());

	m_textDocument->setHtml(text);
}

string MainWindow::getSelectedNickName()
{
	QTableWidgetItem* selected = m_contactList->currentItem();

	if( !selected )
		return string("");
	else
		return selected->text().toStdString();
}

void MainWindow::showWarningDialog(QString title, QString message)
{
	QMessageBox::warning(this, title, message);
}

void MainWindow::showInformationDialog(QString title, QString message)
{
	QMessageBox::information(this, title, message);
}

void MainWindow::closeEvent(QCloseEvent* evt)
{
    cout << "Closing application..." << endl;

	m_owner->shutDown();
}

// SLOTS

void MainWindow::sentMessage()
{
	string message = m_inputBox->document()->toPlainText().toStdString();
	m_inputBox->document()->setPlainText("");

	m_owner->sentMessage(message);
}

void MainWindow::sessionTimeout()
{
	m_owner->checkForResponse();
	m_sessionTimer->start(TIMER_SESSION_INTERVAL);
}

void MainWindow::statusTimeout()
{
	m_owner->findOnlineContacts();
	m_statusTimer->start(TIMER_STATUS_INTERVAL);
}

void MainWindow::createMenuBar()
{
	// Create menubar

	m_menuBar = new MenuBar(m_owner);
	this->setMenuBar(m_menuBar); 

	// Connect slots

	QObject::connect(m_menuBar->m_inviteAction, SIGNAL(triggered()), m_menuBar, SLOT(slotInvite()));
	QObject::connect(m_menuBar->m_closeAction, SIGNAL(triggered()), m_menuBar, SLOT(slotShutDown()));
	QObject::connect(m_menuBar->m_aboutAction, SIGNAL(triggered()), m_menuBar, SLOT(slotAbout()));
}